﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace GoogleAppsTools
{
    static class Program
    {
        static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
        [STAThread]
        static void Main(string[] args)
        {
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new GoogleManager(args));
                mutex.ReleaseMutex();
            }
            else
            {
                if (args.Length > 0)
                {
                    //send a custom message to the other open applications.  This is then picked up by the existing instance of this application and actioned in the WndProc override
                    if (args[0] == "ShowSettings")
                        NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.SHOW_SETTINGS, IntPtr.Zero, IntPtr.Zero);
                    if (args[0] == "CheckForNewMail")
                        NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.CHECK_NEW_MAIL, IntPtr.Zero, IntPtr.Zero);
                    if (args[0] == "CreateNewEvent")
                        NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.NEW_EVENT, IntPtr.Zero, IntPtr.Zero);
                    if (args[0] == "OpenCalendar")
                        NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.OPEN_CALENDAR, IntPtr.Zero, IntPtr.Zero);
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new GoogleManager(args));
                }
            }
        }
    }
}

